package by.ostrovsky.webhousingsystem.junit;

import java.util.LinkedList;
import java.util.List;

import junit.framework.TestCase;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import by.ostrovsky.webhousingsystem.crypt.SHACrypt;
import by.ostrovsky.webhousingsystem.dao.DAOFactory;
import by.ostrovsky.webhousingsystem.dao.implementation.idao.IOperateDAO;
import by.ostrovsky.webhousingsystem.hibernate.entity.Account;

public class RegistrationTest extends TestCase {
	private final int _accessLevel = 2;
	private final String LOGIN = "manager2";
	private final String PASSWORD = "managermanager";
	private final String USERNAME = "Anthony";
	private final String MAIL = "nomail";
	private DAOFactory factory = null;
	private IOperateDAO<List<Account>, Boolean> dao = null;
	private Account account = null;
	private List<Account> list = null;

	@Before
	public void setUp() throws Exception {
		list = new LinkedList<Account>();
		account = new Account();
		factory = DAOFactory.getDAOFactory(DAOFactory.MySQL);
		dao = factory.getAccountDAO();

		account.setId(LOGIN);
		account.setHashed(SHACrypt.getInstance().encrypt(PASSWORD));
		account.setUserName(USERNAME);
		account.setMail(MAIL);
		account.setAccessLevel(_accessLevel);
		list.add(account);
	}

	@After
	public void tearDown(){
		list = null;
		dao = null;
		factory = null;
		account = null;
	}

	@Test
	public void test1() {
		boolean isSuccess;
		try {
			isSuccess = dao.create(list);
			assertEquals(true, isSuccess);
			
		} catch (Exception e) {
			//there is no existance check for login(PK) 
			//so SqlExceptionHelper: "duplicated entry" may happens
			fail(e.toString());
		}
	}
	
	@Test
	public void test2(){
		boolean isSuccess;
		try{
			isSuccess = dao.delete(list);
			assertEquals(true, isSuccess);
		}
		catch(Exception e){
			fail(e.toString());
		}
	}
}
